﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using WeiboSDK;
using WeiboSDK.Model;
using Alexis.WindowsPhone;
using System.Windows.Media.Imaging;

namespace Cnblogs
{
    public partial class SettingPage : PhoneApplicationPage
    {
        public SettingPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            BindWeiboUser();
            toggle_image.IsChecked = GlobalFunc.IsEnableImage;
            toggle_image.Content = GlobalFunc.IsEnableImage ? Constants.L_ToggleOn : Constants.L_ToggleOff;
            toggle_dark.IsChecked = GlobalFunc.IsDarkTheme;
            toggle_dark.Content = GlobalFunc.IsDarkTheme ? Constants.L_ToggleOn : Constants.L_ToggleOff;
            if (GlobalFunc.PageSize == 10)
            {
                lp_pageSize.SelectedIndex = 0;
            }
            else if (GlobalFunc.PageSize == 20)
            {
                lp_pageSize.SelectedIndex = 1;
            }
            else
            {
                lp_pageSize.SelectedIndex = 2;
            }
            base.OnNavigatedTo(e);
        }

        private void toggle_image_Checked(object sender, RoutedEventArgs e)
        {
            GlobalFunc.IsEnableImage = true;
            toggle_image.Content = Constants.L_ToggleOn;
        }

        private void toggle_image_Unchecked(object sender, RoutedEventArgs e)
        {
            GlobalFunc.IsEnableImage = false;
            toggle_image.Content = Constants.L_ToggleOff;
        }

        private void toggle_dark_Checked(object sender, RoutedEventArgs e)
        {
            GlobalFunc.IsDarkTheme = true;
            toggle_dark.Content = Constants.L_ToggleOn;
        }

        private void toggle_dark_Unchecked(object sender, RoutedEventArgs e)
        {
            GlobalFunc.IsDarkTheme = false;
            toggle_dark.Content = Constants.L_ToggleOff;
        }

        private void BindWeiboUser()
        {
            if (App.Token == null || App.Token.ExpiresTime.AddSeconds(30) <= DateTime.Now)
            {
                img_profile.Source = Application.Current.Resources["Img_DefaultProfile"] as BitmapImage;
                hb_bindweibo.Content = Constants.L_BindWeibo;
                tbk_screenname.Text = string.Empty;
            }
            else
            {
                if (App.weiboUser == null)
                {
                    WeiboAPI.Request(RequestType.UserInfo, new RequestData { Token = App.Token }, (data) =>
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(delegate
                        {
                            var p = data as WeiboUser;
                            if (p != null)
                            {
                                App.weiboUser = p;
                                LowProfileImageLoader.SetUriSource(img_profile, new Uri(App.weiboUser.profile_image_url, UriKind.Absolute));
                                hb_bindweibo.Content = Constants.L_UnBindWeibo;
                                tbk_screenname.Text = App.weiboUser.screen_name;
                                //save 
                                JsonHelper.SerializeData<WeiboUser>(WeiboAPI.API_Weibo_User_File, p);
                            }
                        });
                    });
                }
                else
                {
                    LowProfileImageLoader.SetUriSource(img_profile, new Uri(App.weiboUser.profile_image_url, UriKind.Absolute));
                    hb_bindweibo.Content = Constants.L_UnBindWeibo;
                    tbk_screenname.Text = App.weiboUser.screen_name;
                }
            }
        }

        private void hb_bindweibo_Click(object sender, RoutedEventArgs e)
        {
            if (hb_bindweibo.Content != null && hb_bindweibo.Content.ToString() == Constants.L_BindWeibo)
            {
                NavigationService.Navigate(new Uri(Constants.P_LoginWeiboPage, UriKind.Relative));
            }
            else
            {
                if (MessageBox.Show(Constants.L_UnBindTip, "", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    GlobalFunc.LogOffWeibo();
                    Deployment.Current.Dispatcher.BeginInvoke(delegate
                    {
                        img_profile.Source = Application.Current.Resources["Img_DefaultProfile"] as BitmapImage;
                        hb_bindweibo.Content = Constants.L_BindWeibo;
                        tbk_screenname.Text = string.Empty;
                    });
                }
            }
        }

        private void lp_pageSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lp_pageSize != null && lp_pageSize.SelectedIndex != -1)
            {
                switch (lp_pageSize.SelectedIndex)
                {
                    case 0:
                        GlobalFunc.PageSize = 10;
                        break;
                    case 1:
                        GlobalFunc.PageSize = 20;
                        break;
                    case 2:
                        GlobalFunc.PageSize = 50;
                        break;
                    default:
                        break;
                }
            }
        }
    }
}